#!/usr/bin/python
# -*- coding: UTF-8 -*-
import base64
import hashlib
import json
import time

import requests


def main(language, category):
    x_appid = '5d312552'
    api_key = '4cf98e2bb508018d2916bf3f944451a7'
    curTime = str(int(time.time()))
    url = 'http://api.xfyun.cn/v1/service/v1/ise'
    text = "[content]\nwhere are you going?"
    AUDIO_PATH = r'/Users/ddy/Music/audio/WhereAreYouGoing_youdao.wav'
    with open(AUDIO_PATH, 'rb') as f:
        file_content = f.read()
    base64_audio = base64.b64encode(file_content)
    body = {'audio': base64_audio, 'text': text}
    param = json.dumps({"aue": "raw", "result_level": "entirety", "language": language, "category": category})
    paramBase64 = str(base64.b64encode(param.encode('utf-8')), 'utf-8')
    m2 = hashlib.md5()
    m2.update((api_key + curTime + paramBase64).encode('utf-8'))
    checkSum = m2.hexdigest()
    x_header = {
                'X-Appid': x_appid,
                'X-CurTime': curTime,
                'X-Param': paramBase64,
                'X-CheckSum': checkSum,
                'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
                }
    req = requests.post(url, data=body, headers=x_header)
    result = req.content.decode('utf-8')
    print(result)
    return


if __name__ == '__main__':
    language_en = 'en_us'
    language_zh = 'zh_cn'
    category_read_syllable = 'read_syllable'
    category_read_word = 'read_word'
    category_read_sentence = 'read_sentence'
    category_read_chapter = 'read_chapter'
    main(language_en, category_read_sentence)
